<?php
$GLOBALS['basePath'] = '../';
require_once '../class/PHONGBAN.php';
require_once '../class/NHANVIEN.php';
require_once '../class/common.php';
require_once '../class/Paging.php';

function getDepartmentView() {
	$depts = PHONGBAN::Select();
	$result = array();
	foreach ($depts as $dept) {
		$view = $dept;
		$view['SoNV'] = PHONGBAN::GetEmployeeCount($dept['MaPhong']);
		array_push($result, $view);
	}
	return $result;
}

function getEmployeeView($lst){
	$result = array();
	foreach ($lst as $emp) {
		$view = NHANVIEN::GetView($emp);
		array_push($result, $view);
	}
	return $result;
}

try {
	$phong = isset($_GET['phong'])? $_GET['phong']: -1;
	$pageIndex = isset($_GET['page'])? $_GET['page']: 1;
	$rowPerPage = 5;
	
	// POST: remove selected
	if (isset($_POST["removes"])) {
		$values = isset($_POST["MaNV"])? $_POST["MaNV"]: array();
		foreach ($values as $value) {
			$obj = NHANVIEN::Read($value);
			if ($obj) NHANVIEN::Delete($obj);
		}
		Redirect('index.php');
	}
	
	// get department list
	$depts = getDepartmentView();
	
	// get employee list
	$emps = NHANVIEN::Select($phong);
	$paging = new Paging($emps, $pageIndex, $rowPerPage);
	$emps = getEmployeeView($paging->GetPagedList());

	// get paging control
	$pagingNav = $paging->GetPagingNav('pagination');
	$pageCount = $paging->GetPageCount();
} catch (Exception $e) {
	Redirect("/error.php");			
}
EchoHeader('Nhân viên', 'employee');
?>
<script type="text/javascript">
	$(function () {
		// init date-picker control
		$('#NgaySinh').datepicker({
			inline: true,
			dateFormat: "dd/mm/yy"
		});
	});
</script>
	
<style type="text/css">
#HoTen { width: 149px; }

#NgaySinh {	width: 80px; }

#Luong { width: 80px; }

#DiaChi	{ width: 150px; }

.button { font-size: 8pt; }
</style>

<div class="secondary box">
	<div class="box-inside">
		<div class="heading"><h2>Phòng ban</h2></div>
		<ul class="secondary-list">
			<?php
			foreach ($depts as $dept)
				echo sprintf('<li><a href="?phong=%d">%s (%d)</a></li>', $dept['MaPhong'], $dept['TenPhong'], $dept['SoNV']);
			?>
			<li><a href="?">Tất cả (<?php echo PHONGBAN::GetEmployeeCount(); ?>)</a></li>
		</ul>
	</div>
</div>

<div class="primary box">
	<div class="box-inside">
		<form method="post" id="frmDSNV" name="frmDSNV" onSubmit="return onFrmDSNVSubmit()">
		<div class="heading">
			<h2>Danh sách nhân viên</h2>
			<div class="buttons">
				<input type="submit" name="removes" value="Xóa" class="button" />
				<a href="create.php" class="button">Thêm</a>
			</div>
		</div>
		<table class="tablist">
			<tr>
				<th><input type="checkbox" name="CheckAll" onClick="onCheckAll(this)"/></th>
				<th>Họ tên</th>
				<th>Ngày sinh</th>
				<th>Địa chỉ</th>
				<th>Phái</th>
				<th>Lương</th>
				<th>Phòng</th>
				<th></th>
			</tr>
			<?php
				foreach ($emps as $emp) {
					echo '<tr>';
					$record = sprintf('
						<td><input type="checkbox" name="MaNV[]" value="%s" onClick="OnCheck(this)"/></td>
						<td>%s</td>
						<td>%s</td>
						<td>%s</td>
						<td>%s</td>
						<td>%.0f</td>
						<td>%s</td>
						<td>
							<a href="edit.php?Id=%d"><img src="../image/edit.gif" alt="" /></a>
							<a href="delete.php?Id=%d"><img src="../image/delete.gif" alt="" /></a>
						</td>', $emp['MaNV'], $emp['HoTen'], $emp['NgaySinh'], substr($emp['DiaChi'], 0, 30), $emp['Phai'], $emp['Luong'], $emp['Phong'], 
					$emp['MaNV'], $emp['MaNV'], $emp['MaNV']);
					echo $record;
					echo '</tr>';
				}
			?>
			<tr>
				<td><input type="checkbox" name="CheckAll" onClick="onCheckAll(this)"/></td>
				<td><input name="HoTen" id="HoTen" /></td>
				<td><input name="NgaySinh" id="NgaySinh" /></td>
				<td><input name="DiaChi" id="DiaChi" /></td>
				<td>
					<select name="Phai">
						<option value="0">Nam</option>
						<option value="1">Nữ</option>
					</select>
				</td>
				<td><input name="Luong" id="Luong" onkeypress="return checkNumber(this,10,event);"/></td>
				<td>
					<select name="Phong">
					<?php
					foreach ($depts as $dept)
						echo sprintf('<option value="%d">%s</option>', $dept['MaPhong'], $dept['TenPhong']);
					?>
					</select>
				</td>
				<td><input type="button" value="Thêm" onClick="onBtnAdd()" class="button"/></td>
			</tr>

		</table>
		<div class="box-footer" style="overflow: hidden">
			<div style="float: left">Trang <?php echo $pageIndex; ?> trong tổng số <?php echo $pageCount; ?> trang</div>
			<div style="float: right"><?php echo $pagingNav; ?></div>
		</div>
		</form>
	</div>
</div>

<?php EchoFooter(); ?>